package com.twu.biblioteca.menu;

import com.twu.biblioteca.Librarian;
import org.junit.After;

public class CheckoutInfoMenu implements Menu{
    public static final String THANK_YOU_ENJOY_THE_BOOK = "Thank you! Enjoy the book!";
    public static final String SORRY_THAT_BOOK_IS_NOT_AVAILABLE = "Sorry! That book is not available.";
    public static final String THANK_YOU_ENJOY_THE_MOVIE = "Thank you! Enjoy the movie!";
    public static final String SORRY_THAT_MOVIE_IS_NOT_AVAILABLE = "Sorry! That movie is not available.";
    private String type;
    private Librarian librarian;

    public CheckoutInfoMenu(String type,Librarian librarian) {
        this.type = type;
        this.librarian = librarian;
        showMenu();
    }

    @Override
    public void showMenu() {
        if (type.equals("Book")) {
            System.out.println("Please enter the index of the book:");
            librarian.listBooks();
        }
        else if (type.equals("Movie")) {
            System.out.println("Please enter the index of the movie:");
            librarian.listMovies();
        }
    }

    @Override
    public Menu processInput(String input) {
        if (type.equals("Book") && (Integer.parseInt(input) <= librarian.getBookNumbers())) {
            return getMenu(librarian.checkoutBook(input), THANK_YOU_ENJOY_THE_BOOK, SORRY_THAT_BOOK_IS_NOT_AVAILABLE);
        } else if (type.equals("Movie") && (Integer.parseInt(input) <= librarian.getMoviesNumbers())) {
            return getMenu(librarian.checkoutMovie(input), THANK_YOU_ENJOY_THE_MOVIE, SORRY_THAT_MOVIE_IS_NOT_AVAILABLE);
        } else {
            System.out.println("Sorry, please re-enter.");
            return this;
        }
    }

    private Menu getMenu(boolean success, String successWords, String failWords) {
        if (success) {
            System.out.println(successWords);
            return new LibraryMenu(librarian);
        }
        else {
            System.out.println(failWords);
            return new CheckoutMenu(librarian);
        }
    }

    @After
    public void cleanUpStreams() {
        System.setOut(null);
    }
}
